package com.wz.project.ddjqueue.service.impl;

import com.wz.common.utils.text.Convert;
import com.wz.project.ddjqueue.domain.WmsDeviceExecutionQueue;
import com.wz.project.ddjqueue.mapper.WmsDeviceExecutionQueueFinishMapper;
import com.wz.project.ddjqueue.mapper.WmsDeviceExecutionQueueMapper;
import com.wz.project.ddjqueue.service.IWmsDeviceExecutionQueueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 堆垛机执行队列Service业务层处理
 * 
 * @author wld
 * @date 2023-04-06
 */
@Service
public class WmsDeviceExecutionQueueServiceImpl implements IWmsDeviceExecutionQueueService
{
    @Autowired
    private WmsDeviceExecutionQueueMapper wmsDeviceExecutionQueueMapper;

    @Autowired
    private WmsDeviceExecutionQueueFinishMapper wmsDeviceExecutionQueueFinishMapper;
    /**
     * 查询堆垛机执行队列
     * 
     * @param rwh 堆垛机执行队列主键
     * @return 堆垛机执行队列
     */
    @Override
    public WmsDeviceExecutionQueue selectWmsDeviceExecutionQueueByRwh(Long rwh)
    {
        return wmsDeviceExecutionQueueMapper.selectWmsDeviceExecutionQueueByRwh(rwh);
    }

    /**
     * 查询堆垛机执行队列列表
     * 
     * @param wmsDeviceExecutionQueue 堆垛机执行队列
     * @return 堆垛机执行队列
     */
    @Override
    public List<WmsDeviceExecutionQueue> selectWmsDeviceExecutionQueueList(WmsDeviceExecutionQueue wmsDeviceExecutionQueue)
    {
        if(wmsDeviceExecutionQueue.getJdbz()==1) {
            return wmsDeviceExecutionQueueMapper.selectWmsDeviceExecutionQueueList(wmsDeviceExecutionQueue);
        }else{
            return wmsDeviceExecutionQueueFinishMapper.selectWmsDeviceExecutionQueueFinishList(wmsDeviceExecutionQueue);
        }
    }



    /**
     * 新增堆垛机执行队列
     * 
     * @param wmsDeviceExecutionQueue 堆垛机执行队列
     * @return 结果
     */
    @Override
    public int insertWmsDeviceExecutionQueue(WmsDeviceExecutionQueue wmsDeviceExecutionQueue)
    {
        return wmsDeviceExecutionQueueMapper.insertWmsDeviceExecutionQueue(wmsDeviceExecutionQueue);
    }

    /**
     * 修改堆垛机执行队列
     * 
     * @param wmsDeviceExecutionQueue 堆垛机执行队列
     * @return 结果
     */
    @Override
    public int updateWmsDeviceExecutionQueue(WmsDeviceExecutionQueue wmsDeviceExecutionQueue)
    {
        return wmsDeviceExecutionQueueMapper.updateWmsDeviceExecutionQueue(wmsDeviceExecutionQueue);
    }

    /**
     * 批量删除堆垛机执行队列
     * 
     * @param rwhs 需要删除的堆垛机执行队列主键
     * @return 结果
     */
    @Override
    public int deleteWmsDeviceExecutionQueueByRwhs(String rwhs)
    {
        return wmsDeviceExecutionQueueMapper.deleteWmsDeviceExecutionQueueByRwhs(Convert.toStrArray(rwhs));
    }

    /**
     * 删除堆垛机执行队列信息
     * 
     * @param queue 堆垛机执行队列
     * @return 结果
     */
    @Override
    public int deleteTask(WmsDeviceExecutionQueue queue) {

        int sa = 0;

        WmsDeviceExecutionQueue ddjQueue = wmsDeviceExecutionQueueMapper.getQueueByTask(queue);
        if(ddjQueue == null){
            return sa;
        }else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            ddjQueue.setJssj(sdf.format(new Date()));
            ddjQueue.setDdjxf("2");
            wmsDeviceExecutionQueueFinishMapper.insertWmsDeviceExecutionQueue(ddjQueue);
            sa = wmsDeviceExecutionQueueMapper.deleteWmsDeviceExecutionQueueByRwh(queue.getRwh());
            return sa;
        }
    }

    @Override
    public int reloadTask(WmsDeviceExecutionQueue queue) {
        WmsDeviceExecutionQueue wmsDeviceExecutionQueue = wmsDeviceExecutionQueueMapper.selectWmsDeviceExecutionQueueByRwh(queue.getRwh());
        if(wmsDeviceExecutionQueue !=null) {
            queue.setDdjxf("0");

            return wmsDeviceExecutionQueueMapper.updateWmsDeviceExecutionQueue(queue);
        }else{
            return 0;
        }
    }
}
